package pers.lipeng.snake;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class SnakeBody extends JLabel {
    private int bodyX;
    private int bodyY;

    public SnakeBody(int bodyX, int bodyY) {
        this.bodyX = bodyX;
        this.bodyY = bodyY;
        loadIcon(false);
    }

    public SnakeBody(int bodyX,int bodyY,boolean isHead){
        this.bodyX = bodyX;
        this.bodyY = bodyY;
        loadIcon(isHead);
    }

    public int getBodyX() {
        return bodyX;
    }

    public void setBodyX(int bodyX) {
        this.bodyX = bodyX;
    }

    public int getBodyY() {
        return bodyY;
    }

    public void setBodyY(int bodyY) {
        this.bodyY = bodyY;
    }


    private void loadIcon(boolean isHead){
        URL path = null;
        if(isHead){
            path = getClass().getResource("/snake/img/head.png");
        }else{
            path = getClass().getResource("/snake/img/food.png");
        }
        int width = this.getWidth();
        int height = this.getHeight();
        if (width == 0 || height == 0){
            width = 15;
            height = 15;
        }
        ImageIcon imageIcon = new ImageIcon(path);
        Image image = imageIcon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT);
        imageIcon.setImage(image);
        this.setIcon(imageIcon);
    }
}
